package com.hzh.page.controller.admin;


import com.hzh.page.common.enums.RightEnum;
import com.hzh.page.common.exception.ResponseEnum;
import com.hzh.page.common.utils.R;
import com.hzh.page.pojo.AuthenticationMenuEntity;
import com.hzh.page.service.AuthenticationMenuService;
import com.hzh.page.vo.AuthenticationMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 胡泽华
 */
@Api(tags = "菜单权限信息")
@RestController
@RequestMapping(value = "/page/admin/menu")
public class AuthenticationMenuController {
    @Resource
    private AuthenticationMenuService authenticationMenuService;
    @Autowired
    private AuthenticationMenuService menuService;

    /**
     * 查询菜单权限
     *
     * @return R
     */
    @ApiOperation("权限列表--父子分级")
    @GetMapping("/list")
    public R listWithLevel() {
        List<AuthenticationMenuVO> menuEntities = authenticationMenuService.listAuthenticationMenu();
        return R.ok().put("data", menuEntities);
    }

    /**
     * 查询菜单权限--不做父子分级
     *
     * @return R
     */
    @ApiOperation("权限列表--不做父子分级")
    @GetMapping("/listAll")
    public R list() {
        List<AuthenticationMenuEntity> list = authenticationMenuService.list();
        return R.ok().put("data", list);
    }

    /**
     * 删除菜单权限
     *
     * @return R
     */
    @ApiOperation("删除权限")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id, HttpServletRequest request) {
        boolean hashAuthority = menuService.hashAuthority(request, RightEnum.RIGHT_MANAGE_ROLE_DELETE.getRoute_key());
        if (!hashAuthority) {
            return R.error(ResponseEnum.NO_RIGHT.getMessage());
        }
        AuthenticationMenuEntity menuEntity = authenticationMenuService.getById(id);
        if (menuEntity == null) {
            return R.error("删除失败，该菜单权限不存在");
        }
        authenticationMenuService.deleteAuthenticationMenu(id);
        return R.ok("删除成功");
    }

    /**
     * 更新菜单权限是否展示
     *
     * @return R
     */
    @ApiOperation("更新权限")
    @PostMapping("/update")
    public R update(@RequestBody AuthenticationMenuVO menuVO, HttpServletRequest request) {
        boolean hashAuthority = menuService.hashAuthority(request, RightEnum.RIGHT_MANAGE_ROLE_UPDATE.getRoute_key());
        if (!hashAuthority) {
            return R.error(ResponseEnum.NO_RIGHT.getMessage());
        }
        authenticationMenuService.updateAuthenticationMenu(menuVO.getId(), menuVO.getPagepermisson());
        return R.ok("修改成功");
    }


}
